package com.sk.skplanet.commute.actions;

import java.io.IOException;
import java.io.StringReader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.sk.skplanet.commute.controllers.DialogController;
import com.sk.skplanet.commute.controllers.SeoulBusApiController;
import com.sk.skplanet.commute.vo.API_Businfo;
import com.sk.skplanet.commute.vo.Data;
import com.sk.skplanet.commute.vo.StopInfo;

public class API_searchBus extends AsyncTask<String, Integer, String> {

	private Context context;
	private int routePostion;
	private int pointPosition;
	private StopInfo stopInfo;
	private String busId;

	public API_searchBus(Context context, int routePostion, int pointPosition, StopInfo stopInfo) {
		super();
		this.context = context;
		this.routePostion = routePostion;
		this.pointPosition = pointPosition;
		this.stopInfo = stopInfo;
	}

	public API_searchBus(Context context, String busId) {
		this.context = context;
		this.busId = busId;
	}

	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);

		try {
			XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
			XmlPullParser parser = parserCreator.newPullParser();

			parser.setInput(new StringReader(result));
			parser.next();

			int parserEvent = parser.getEventType();

			String tagName = "";

			Data.api_businfos.clear();
			API_Businfo api_businfo = null;
			while (parserEvent != XmlPullParser.END_DOCUMENT) {
				switch (parserEvent) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					tagName = parser.getName();
					break;
				case XmlPullParser.END_TAG:
					break;
				case XmlPullParser.TEXT:
					if ("busRouteId".equals(tagName)) {
						if (api_businfo == null) api_businfo = new API_Businfo(parser.getText());
//						Log.i("parser", "시스템번호 : " + parser.getText());
					}
					if ("busRouteNm".equals(tagName)) {
						if (api_businfo != null) api_businfo.setBusName(parser.getText());
//						Log.i("parser", "번호 : " + parser.getText());
					}
					if ("busRouteType".equals(tagName)) {
						if (api_businfo != null) {
							api_businfo.setBusType(parser.getText());
							Data.api_businfos.add(api_businfo);
							api_businfo = null;
						}
//						Log.i("parser", "버스종류 : " + parser.getText());
					}
					break;
				}

				parserEvent = parser.next();
			}

			DialogController.createBusDialog(context, routePostion, pointPosition, stopInfo).show();
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
	}

	@Override
	protected String doInBackground(String... arg0) {
		String result = "결과가 없습니다.";
		SeoulBusApiController sbac = new SeoulBusApiController("");

		try {
			if ("getRouteByStation".equals(arg0[0])) {
				result = sbac.getRouteByStation(arg0[1]);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return result;

	}
}